<?php
namespace app\lib\exception;

use Exception;
use think\exception\Handle;

class ExceptionHandler extends Handle
{
    public $code;
    public $msg;
    public $errCode;

    public function render(Exception $e){
        // halt($e);

        if ($e instanceof BaseException) {
            $this->code = $e->code;
            $this->msg = $e->msg;
            $this->errCode = $e->errCode;
        } else {

            // debug 开启 显示默认异常
            if (config('app.app_debug')) {
                return parent::render($e);
            }

            $this->code = 500;
            $this->msg = '服务器异常';
            $this->errCode = '999';
        }

        $res = [
            'msg' => $this->msg,
            'errCode' => $this->errCode
        ];

        return json($res, $this->code);
    }
}
